
#include "DIMouse.h"

CDIMouse::CDIMouse()
{
	memset( &m_State,0,sizeof(DIMOUSESTATE2) );
	memset( &m_StateLast,0,sizeof(DIMOUSESTATE2) );

	GetCursorPos( &m_Position );
	m_PositionLast = m_Position;
}

CDIMouse::~CDIMouse()
{
}

bool CDIMouse::Init( HWND hWnd )
{
	HRESULT hr = S_OK;

	hr = m_pDevice->SetDataFormat(&c_dfDIMouse2);
	hr = m_pDevice->SetCooperativeLevel( hWnd, DISCL_FOREGROUND|DISCL_NONEXCLUSIVE );				
	
	hr = m_pDevice->Acquire();

	return TRUE;
}

bool CDIMouse::Update()
{
	m_PositionLast = m_Position;
	GetCursorPos( &m_Position );
	
	HRESULT hr = m_pDevice->Acquire();

	if(FAILED(hr))
	{
		return false;
	}
	else
	{
		memcpy( &m_StateLast,&m_State,sizeof(DIMOUSESTATE2) );
		m_pDevice->GetDeviceState( sizeof(DIMOUSESTATE2),&m_State);
	}

	return true;
}

LONG CDIMouse::GetMouseMoveX()
{
	LONG move = m_State.lX;
	return move;
}

LONG CDIMouse::GetMouseMoveY()
{
	LONG move = m_State.lY;
	return move;
}

LONG CDIMouse::GetMouseMoveZ()
{
	LONG move = m_State.lZ ;
	return move;
}

bool CDIMouse::IsButtonDown( int btn )
{
	UINT stat = ButtonPressed( m_State.rgbButtons[btn]) ? 1:0;
	UINT stat_last = ButtonPressed( m_StateLast.rgbButtons[btn]) ? 1:0;
	
	bool stat_new = ButtonDown( stat,stat_last )?true:false;
	return stat_new;
}

bool CDIMouse::IsButtonUp( int btn )
{
	UINT stat = ButtonPressed( m_State.rgbButtons[btn]) ? 1:0;
	UINT stat_last = ButtonPressed( m_StateLast.rgbButtons[btn]) ? 1:0;
	
	bool stat_new = ButtonUp( stat,stat_last )?true:false;
	return stat_new;
}

bool CDIMouse::IsButtonPressed( int btn )
{
	bool stat = ButtonPressed( m_State.rgbButtons[btn]) ? true:false;
	return stat;
}

bool CDIMouse::IsLeftButtonDown()
{
	return IsButtonDown( 0) ;
}

bool CDIMouse::IsLeftButtonUp()
{
	return IsButtonUp( 0) ;
}

bool CDIMouse::IsLeftButtonPressed()
{
	return IsButtonPressed( 0) ;
}

bool CDIMouse::IsRightButtonDown()
{
	return IsButtonDown( 1 ) ;
}

bool CDIMouse::IsRightButtonUp()
{
	return IsButtonUp( 1 ) ;
}

bool CDIMouse::IsRightButtonPressed()
{
	return IsButtonPressed( 1 ) ;
}

bool CDIMouse::IsMiddleButtonDown()
{
	return IsButtonDown( 2) ;
}

bool CDIMouse::IsMiddleButtonUp()
{
	return IsButtonUp( 2 ) ;
}

bool CDIMouse::IsMiddleButtonPressed()
{
	return IsButtonPressed( 2 ) ;
}